package com.sxt;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sxt.copy.Jsoup1;
import com.sxt.copy.Jsoup2;
import com.sxt.entity.Article;
import com.sxt.service.ArticleService;

public class bishijie {
	 // 获取img标签正则
    private static final String IMGURL_REG = "<img.*src=(.*?)[^>]*?>";
	private final static String BiUrl = "http://www.bishijie.com/shendu";
	private static String biUrl = "http://www.bishijie.com/shendu_";
	public static List<String> getUrl() throws IOException{
		Document doc = Jsoup.connect(BiUrl).get();
		Elements els = doc.select("div.container");
		List<String> list = new ArrayList<String>();
		for (Element element : els) {
			String pageView = element.select("span.pageView").text();
			int count = Integer.parseInt(pageView);
			if(count>1){
			String href = element.select("div.container").attr("id");
			  list.add(biUrl+href);
			}
		}
		return list;
	}
	public static String getFirstImg(String content){
		Matcher matcher=Pattern.compile(IMGURL_REG).matcher(content);
		if(matcher.find()==false){
			
			return "";
		}else{	
		String FirstImg = matcher.group();
		Document doc = Jsoup.parse(FirstImg);
   	    Elements link = doc.select("img");
   	    String src = link.attr("src");
		return src;
		}
	}
    public static void add(String url){
    	 List<String> list = Jsoup1.addArticle(url, 
					"h2.title", 
					"div.content");
    	 
 	//启动Spring容器
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
		ArticleService articleService=(ArticleService) context.getBean("articleService");
		Article art =new Article();
		
		String id =String.valueOf((int) (Math.random()*1000000000));
       art.setId(id);
       art.setTitle(list.get(0));
       art.setTags("EOS,ETH,BTC,区块链");
       art.setAuthorId("1536799125217");
       art.setContent(list.get(1));
       art.setCommentCount(0);
       art.setViewCount(0);
       art.setRewardContent("");
       art.setRewardPoint(0);
       art.setPermalink("/article/"+art.getId());
       Long l = System.currentTimeMillis();
       String date = l.toString();
       art.setCreateTime(new BigInteger(date));
       art.setUpdateTime(new BigInteger(date));
       art.setLatestCmtTime(new BigInteger("0"));
       art.setLatestCmterName("");
       art.setRandomDouble(Math.random());
       art.setCommentable("1");
       art.setEditorType(0);
       art.setArticleStatus(0);
       art.setArticleType(0);
       art.setGoodCnt(0);
       art.setBadCnt(0);
       art.setCollectCnt(0);
       art.setWatchCnt(0);
       art.setRedditScore(0d);
       art.setCity("纽约");
       art.setIP("55.55.55.55");
       art.setUA("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0");
       art.setStick(new BigInteger("0"));
       art.setAnonymous(0);
       art.setPerfect(0);
       art.setAnonymousView(0);
       art.setAudioURL("");
       art.setQnAOfferPoint(0);
       art.setPushOrder(0);
       art.setImg1URL(bishijie.getFirstImg(list.get(1)));
       articleService.register(art);
    	
    	
    }
    
	public static void main(String[] args) throws Exception{
		 for (String url : getUrl()) {
		    	 add(url);
			}
		/*List<String> urls = new ArrayList<String>();
		 
		 urls.add("http://www.bishijie.com/shendu_3006");
		 urls.add("http://www.bishijie.com/shendu_3005");
		 urls.add("http://www.bishijie.com/shendu_3003");
		 urls.add("http://www.bishijie.com/shendu_3000");
		 urls.add("http://www.bishijie.com/shendu_2976");
		 urls.add("http://www.bishijie.com/shendu_2964");
		 urls.add("http://www.bishijie.com/shendu_2961");
		 urls.add("http://www.bishijie.com/shendu_2958");
		 urls.add("http://www.bishijie.com/shendu_2956");
		 urls.add("http://www.bishijie.com/shendu_2942");
		 urls.add("http://www.bishijie.com/shendu_2935");
		 urls.add("http://www.bishijie.com/shendu_2925");
		 urls.add("http://www.bishijie.com/shendu_2916");
		 urls.add("http://www.bishijie.com/shendu_2890");
		 urls.add("http://www.bishijie.com/shendu_2889");
		 urls.add("http://www.bishijie.com/shendu_2866");
		 urls.add("http://www.bishijie.com/shendu_2847");
		 urls.add("http://www.bishijie.com/shendu_2832");
		 urls.add("http://www.bishijie.com/shendu_2828");
		 urls.add("http://www.bishijie.com/shendu_2824");
		 urls.add("http://www.bishijie.com/shendu_2808");
		 urls.add("http://www.bishijie.com/shendu_2801");
		 urls.add("http://www.bishijie.com/shendu_2717");
		 urls.add("http://www.bishijie.com/shendu_2715");
		 urls.add("http://www.bishijie.com/shendu_2699");
		 urls.add("http://www.bishijie.com/shendu_2684");
		 urls.add("http://www.bishijie.com/shendu_2671");
		 urls.add("http://www.bishijie.com/shendu_2666");
		 urls.add("http://www.bishijie.com/shendu_4658");
		 
		 
		 
		
		
		 
		 
		 
		
		 
		 
		for (String url : urls) {
			 add(url);
		}*/
     
     }
}
